<template>
  <div class="cart">
    <h2>商品列表</h2>
    <div v-for="product in products" :key="product.id">
      {{ product.name }} - ￥{{ product.price }}
      <button @click="addToCart(product)">加入购物车</button>
    </div>

    <h2>购物车</h2>
    <ul>
      <li v-for="item in cartItems" :key="item.id">
        {{ item.name }} × {{ item.quantity }}
        <button @click="removeFromCart(item.id)">删除一个</button>
      </li>
    </ul>

    <h3>总价：￥{{ totalPrice }}</h3>
  </div>
</template>

<script>
import { mapActions, mapGetters } from 'vuex'

export default {
  data() {
    return {
      products: [
        { id: 1, name: '苹果', price: 5 },
        { id: 2, name: '香蕉', price: 3 },
        { id: 3, name: '橙子', price: 4 }
      ]
    }
  },
  methods: {
    ...mapActions(['addToCart', 'removeFromCart'])
  },
  computed: {
    ...mapGetters(['cartItems', 'totalPrice'])
  }
}
</script>